package com.example.juejincheck.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.juejincheck.dao.KongqizhaguoCaipuDao;
import com.example.juejincheck.dao.MotianlunAccountDao;
import com.example.juejincheck.domain.KongqizhaguoCaipu;
import com.example.juejincheck.utils.TianxingUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@RestController
@RequestMapping("/msg_callback")
public class MsgCallbackController {

    private Logger log = LoggerFactory.getLogger(MsgCallbackController.class);
    @Resource
    private KongqizhaguoCaipuDao kongqizhaguoCaipuDao;

    @PostMapping(value = "/send")
    public void send(@RequestBody JSONObject jsonObject, HttpServletResponse response) {
        log.info("jsonObject:{}", jsonObject);
        String word = jsonObject.getString("word");
        String result = "";
        if(word.contains("菜谱")){
            String caipu = word.replaceAll("菜谱", "");
            result = TianxingUtils.getCaipu(caipu);
        }else if(word.contains("空气炸锅")){
            String caipu = word.replaceAll("空气炸锅", "");
//            result = TianxingUtils.getCaipu(caipu);
            List<KongqizhaguoCaipu> list = kongqizhaguoCaipuDao.selectByName(caipu);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                KongqizhaguoCaipu kongqizhaguoCaipu = list.get(i);
                sb.append((i+1)+"、")
                        .append(kongqizhaguoCaipu.getName())
                        .append("：\n\n")
                        .append("【温度时间】")
                        .append(kongqizhaguoCaipu.getMethodContent())
                        .append("\n\n");
                if(!StringUtils.isEmpty(kongqizhaguoCaipu.getZuofa())){
                    sb.append("【做法】")
                            .append(kongqizhaguoCaipu.getZuofa())
                            .append("\n\n");
                }
            }
            result = sb.toString();
        }
        if(StringUtils.isEmpty(result)){
            result = "没有查到你说的哟！";
        }
        JSONObject res = new JSONObject();
        res.put("code", 200);
        res.put("msg", "success");
        JSONArray array = new JSONArray();
        JSONObject data = new JSONObject();
        data.put("type", 1);
        data.put("content", result);
        array.add(data);
        res.put("data", array);
        response.reset();
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(JSONObject.toJSONString(res));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                writer.close();
        }
    }
}
